package com.yisu.auction.manager.website.controller.biReportInfo;

import com.google.common.collect.Lists;
import com.yisu.auction.ewf.common.enums.ResponseCode;
import com.yisu.auction.ewf.common.web.JsonResult;
import com.yisu.auction.ewf.entity.BiDataCollect;
import com.yisu.auction.manager.core.service.IDataCollectService;
import com.yisu.auction.manager.website.controller.BaseController;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.function.Function1;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * author: 赵杰
 * Business Description：BI报表——数据汇总业务
 * createTime：2017/9/28
 */
@Controller
@RequestMapping(value = "/dateCollect")
public class DataCollectController extends BaseController {
    @Resource
    private IDataCollectService iDataCollectService;

    /**
     * 初始数据汇总总表
     *
     * @param model 页面传参
     * @return 页面
     */
    @RequestMapping(value = "/initDataCollectReport.json")
    @ResponseBody
    public JsonResult initDataCollectReport(Model model, final Integer days) {
        ExecutorService executorService= Executors.newFixedThreadPool(1);
        Runnable syncRunnable=new Runnable() {
            @Override
            public void run() {
                iDataCollectService.addDataCollect(days);
            }
        };
        executorService.execute(syncRunnable);
        return new JsonResult(ResponseCode.SUCCESS.code, ResponseCode.SUCCESS.msg);
    }


    /**
     * author: 赵杰
     * Business Description：数据汇总报表首页
     *
     * @param model
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index.htm")
    public String index(Model model, HttpServletRequest request) throws Exception {
        try {
            List<BiDataCollect> tempData=Lists.newArrayList();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            List<String> todayBuyMoney = Lists.newArrayList();
            List<String> arrayDate = Lists.newArrayList();
            List<String> memberNewCountArray = Lists.newArrayList();
            List<BiDataCollect> biDataCollects = iDataCollectService.getBiDataCollect();
            tempData=biDataCollects;
            biDataCollects= Linq4j.asEnumerable(biDataCollects).orderBy(new Function1<BiDataCollect, Comparable>() {
                @Override
                public Comparable apply(BiDataCollect biDataCollect) {
                    return biDataCollect.getCollectDate();
                }
            }).toList();
            for (BiDataCollect item : biDataCollects) {
                memberNewCountArray.add(String.valueOf(item.getMemberNewCount()));
                todayBuyMoney.add(String.valueOf(item.getTodayBuyMoney()));
                arrayDate.add("'"+simpleDateFormat.format(item.getCollectDate())+"'");
            }
            model.addAttribute("biDataCollects", tempData);
            model.addAttribute("todayBuyMoney",  StringUtils.join(todayBuyMoney, ","));
            model.addAttribute("DateStr", StringUtils.join(arrayDate, ","));
            model.addAttribute("memberNewCountArray", memberNewCountArray);
        } catch (Exception e) {
            logger.error("数据汇总报表首页异常：" + e.getMessage());
        }
        return "/biReportInfo/dataCollect/index";
    }
}
